package com.yksj.healthtalk.ui.home;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.FriendUtil;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 申请退款 填写资料
 * @author jack_tang
 *
 */
public class RefundActivity extends  BaseFragmentActivity implements OnClickListener {
	private String tel;
	private String account;
	private EditText mTelephone;
	private EditText mPayAcountAgain;
	private EditText mPayAcount;
	private Button mSubmitBtn;
	private String customerId;
	private String payId="";
	private boolean mBankCand;//银行卡
	private String openbank="";
	private EditText mPayName;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.refund);
		initWidget();
		initData();
	}

	private void initData() {
//		bundle.putString("account", response.getString("PAY_ACCOUNT"));
//		bundle.putString("tel", response.getString("TRANSFER_GETTELE"));
		//接收数据
		Intent intent = getIntent();
		titleTextV.setText(R.string.refund);
		if (intent.hasExtra("account")) {
			account = intent.getStringExtra("account");
		}
		if (intent.hasExtra("tel")) {
			tel = intent.getStringExtra("tel");
		}
		
		if (intent.hasExtra("customerId")) {
			customerId = intent.getStringExtra("customerId");
		}
		
		if (intent.hasExtra("payId")) {
			payId = intent.getStringExtra("payId");
		}
		if (intent.hasExtra("payId")) {
			openbank = intent.getStringExtra("TRANSFER_NAME");
		}
		if (intent.hasExtra("bankcard")) {
			mBankCand = intent.getBooleanExtra("bankcard", false);
		}

		
		//初始化数据
		
		if(mBankCand){
			findViewById(R.id.layout_name).setVisibility(View.VISIBLE);
			mPayAcount.setHint("请输入银行卡账号");
			mPayAcountAgain.setHint("请输入银行卡开户行");
		}else{
			mPayAcount.setHint("请输入支付宝账号");
			mPayAcountAgain.setHint("请确认支付宝账号");
		}
		
		
		if (!TextUtils.isEmpty(account)) {
			mPayAcount.setText(account.trim());
			if(!mBankCand){
			mPayAcountAgain.setText(account.trim());
			}else{
				mPayAcountAgain.setText(openbank.trim());	
			}
		}
		
		if (!TextUtils.isEmpty(tel)) {
			mTelephone.setText(tel.trim());
		}
		
		
		
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		mPayAcount = (EditText)findViewById(R.id.pay_acount);
		mPayAcountAgain = (EditText)findViewById(R.id.pay_acount_again);
		mTelephone = (EditText)findViewById(R.id.telephone);
		mSubmitBtn = (Button)findViewById(R.id.submit);
		mPayName=(EditText)findViewById(R.id.pay_name);
		
		
		mSubmitBtn.setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
		
		

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.submit:
			String account = mPayAcount.getText().toString().trim();
			String accountAgain = mPayAcountAgain.getText().toString().trim();
			String name="";
			if(mBankCand)
			name = mPayName.getText().toString().trim();
			String tel = mTelephone.getText().toString().trim();
			if (!SystemUtils.checkPhone(tel)) {
				ToastUtil.showShort(getApplicationContext(), "您输入的手机格式不对!");
				return;
			}
			String result = FriendUtil.inputVerify(account, accountAgain, tel);
			if(!mBankCand){
			if (!TextUtils.isEmpty(result)) {
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  result);
				return;
			}}else{//银行卡
				if (TextUtils.isEmpty(name)) {
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "请您输入收款人姓名");
					return;
				}
				if (TextUtils.isEmpty(account)) {
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "请您输入银行卡账号");
					return;
				}
				if (TextUtils.isEmpty(accountAgain)) {
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "请您输入银行卡开户行");
					return;
				}

			}
			HttpRestClient.doHttpRefundSubmit(SmartFoxClient.getLoginUserId(), customerId, tel, account, payId,mBankCand,openbank,name,new JsonHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, JSONObject response) {
//					{"error_code":"0","error_message":"没有此退款信息"}
					//成功 {}
				if (response.has("error_message")) {
					try {
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  response.getString("error_message"));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					boolean b = response.keys().hasNext();
					if (!b) {
						ToastUtil.showToastPanl("提交完成");
						onBackPressed();
					}
				}
					super.onSuccess(statusCode, response);
				}
			});
			break;
		}
		
	}
	

}
